class SqlBuilder {

	insertRowCovert(tableName, columns, rows) {
		let temp = [];
		temp.push(`insert INTO ${tableName} (`);
		for (const c of columns) {
			temp.push(c.field)
			temp.push(',')
		}
		temp = temp.slice(0, -1)
		temp.push(`) VALUES  `);
		let value = "";
		for (const row of rows) {
			temp.push(' (')
			for (const c of columns) {
				value = this.covertValue(row[c.field], c)
				if (value === '') {
					value = 'null'
				}
				temp.push(value)
				temp.push(',')
			}
			temp = temp.slice(0, -1)
			temp.push(' )')
			temp.push(' ,')
		}
		temp = temp.slice(0, -1)
		temp.push(`;`);
		return temp.join("");

	}
	updateRowCovert(tableName, columns, rows) {
		const arrs = [];
		let temp = [];
		let value = "";
		const headContent = `UPDATE ${tableName} `
		for (const row of rows) {
			temp.push(headContent);
			temp.push(' SET ')
			const primaryKeyValues = row.primaryKeyValues;
			for (const c of columns) {
				value = this.covertValue(row[c.field], c)
				if (value === '') {
					value = 'null'
				}
				temp.push(` ${c.field}=${value}`)
				temp.push(',')
			}
			temp = temp.slice(0, -1)
			temp.push(" WHERE ")
			columns.filter(c => c.primaryKey).forEach(element => {
				const value = primaryKeyValues[element.field];
				if (value) {
					temp.push(`${element.field}=${this.covertValue(value,element)}`)
				} else {
					temp.push(`${element.field} is NULL`)
				}
				temp.push(' AND ');
			});
			temp = temp.slice(0, -1)
			temp.push(`;`);
			arrs.push(temp.join(""));
		}
		return arrs;

	}
	deleteRowCovert(tableName, columns, rows) {
		const arrs = [];
		let temp = [];
		const headContent = `DELETE FROM  ${tableName} `
		for (const row of rows) {
			temp.push(headContent);
			const primaryKeyValues = row.primaryKeyValues;
			temp.push(" WHERE ")
			columns.filter(c => c.primaryKey).forEach(element => {
				const value = primaryKeyValues[element.field];
				if (value) {
					temp.push(`${element.field}=${this.covertValue(value,element)}`)
				} else {
					temp.push(`${element.field} is NULL`)
				}
				temp.push(' AND ');
			});
			temp = temp.slice(0, -1)
			temp.push(`;`);
			arrs.push(temp.join(""));
		}
		return arrs;
	}


	createDatabaseCovert(catalogName, charset, collation) {
		if (!catalogName) {
			return "";
		}
		const arr = [];
		//CREATE DATABASE  `wordpress` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
		arr.push('CREATE DATABASE ');
		arr.push('`' + catalogName + '` ');
		if (charset) {
			arr.push(` DEFAULT CHARACTER SET ${charset} `);
		}
		if (collation) {
			arr.push(` COLLATE ${collation} `);
		}
		return arr.join("");

	}

	alterDatabaseCovert(catalogName, charset, collation) {
		if (!catalogName) {
			return "";
		}
		const arr = [];
		//CREATE DATABASE  `wordpress` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
		arr.push('ALTER DATABASE ');
		arr.push('`' + catalogName + '` ');
		if (charset) {
			arr.push(` DEFAULT CHARACTER SET ${charset} `);
		}
		if (collation) {
			arr.push(` COLLATE ${collation} `);
		}
		return arr.join("");
	}

    /**
     * 删除数据库
     */
	deleteDatabaseCovert(catalogName) {
		if (!catalogName) {
			return "";
		}
        return `DROP DATABASE \`${catalogName}\` `
	}




	covertValue(value, column) {
		if (value == null || value == undefined) {
			return ''
		}
		if (column.type === 'NUMBER') {
			return value;
		}
		return `'${value}'`


	}

}

export {
	SqlBuilder
}
